﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.IO;
using System.Threading;
using System.Net;
using TrackingServerCore;

namespace TrackingService
{
    public partial class TrackingServer : ServiceBase
    {
        private delegate void UpdateStatusCallback(string strMessage);

        private Tracking objTrack;
        private Timer stateTimer;
        private TimerCallback timerDelegate;
        private string ipString;

        public TrackingServer()
        {
            InitializeComponent();

            if (!System.Diagnostics.EventLog.SourceExists("DoDyLogSourse"))
                System.Diagnostics.EventLog.CreateEventSource("DoDyLogSourse", "DoDyLog");
            eventLog1.Source = "DoDyLogSourse";
            eventLog1.Log = "DoDyLog";
            
            ipString = GetIP();
        }

        #region Initialize start listenning

        private string GetIP()
        {
            string strHostName = "";
            strHostName = System.Net.Dns.GetHostName();

            IPHostEntry ipEntry = System.Net.Dns.GetHostEntry(strHostName);

            IPAddress[] addr = ipEntry.AddressList;

            return addr[addr.Length - 1].ToString();
        }

        private void StartListening()
        {
            eventLog1.WriteEntry("Tracking service started in " + ipString);

            // Parse the server's IP address out of the TextBox
            IPAddress ipAddr = IPAddress.Parse(ipString);
            // Create a new instance of the ChatServer object
            ChatServer mainServer = new ChatServer(ipAddr);
            // Hook the StatusChanged event handler to mainServer_StatusChanged
            ChatServer.StatusChanged += new StatusChangedEventHandler(mainServer_StatusChanged);
            // Start listening for connections
            mainServer.StartListening();

            // Show that we started to listen for connections
            eventLog1.WriteEntry(ipString + " is monitoring for connections ...");
        }

        #endregion

        protected override void OnStart(string[] args)
        {
            StartListening();
        }

        public void mainServer_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            // Call the method that updates the form
            //this.Invoke(new UpdateStatusCallback(this.UpdateStatus), new object[] { e.EventMessage });
            eventLog1.WriteEntry(e.EventMessage);
        }

        //private void UpdateStatus(string strMessage)
        //{
        //    // Updates the log with the message
        //    eventLog1.WriteEntry(strMessage);
        //}

        protected override void OnStop()
        {
            eventLog1.WriteEntry("Tracking service stopped");
            
            //timer1.Enabled = false;
            //TextWriter file = new StreamWriter("C:\\logfile1.txt", true);
            //file.WriteLine("Service stopped");
            //file.Close();

            //stateTimer.Dispose();
        }

        //private void timer1_Tick(object sender, System.EventArgs e)
        //{
        //    TextWriter file = new StreamWriter("C:\\logfile1.txt", true);
        //    file.WriteLine("CPU Usage: " + performanceCounter1.NextValue().ToString() + " " + System.DateTime.Now.ToString());
        //    file.Close();
        //}
    }
}
